package xyf.p09.bfs_dfs;

/**
 * ClassName: P1306_Jump3
 * Description:
 * Author: xyf
 * Date: 9/9/22 9:28 AM
 * Version: 1.0
 **/
public class P1306_Jump3
{
    boolean[] visited;
    boolean res = false;

    public boolean canReach(int[] arr, int start)
    {
        visited = new boolean[arr.length];

        this.dfs(arr, start);

        return res;
    }

    private void dfs(int[] arr, int start)
    {
        if (res || start < 0 || start >= arr.length || visited[start])
        {
            return;
        }

        visited[start] = true;
        if (arr[start] == 0)
        {
            res = true;
            return;
        }
        this.dfs(arr, start - arr[start]);
        this.dfs(arr, start + arr[start]);
    }
}